package flow.service;

import flow.model.FlowBusiness;
import flow.model.FlowSection;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.service.Service;
import m.system.exception.MException;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;
import manage.util.DictionaryUtil;

import java.util.Date;

public class FlowBusinessService extends Service {
	/**
	 * 转换成流程业务信息
	 *  oid
	 *  type
	 *  title
	 * 
	 *  Exception
	 */
	public FlowBusiness toFlowBusiness(String adminOid,String deptOid,String oid,String type,String title,String service) throws Exception {
		if(StringUtil.isSpace(oid)) throw new MException(this.getClass(), "业务主键不能为空");
		FlowBusiness model=ModelQueryList.getModel(FlowBusiness.class, oid,"*","applyUser.*","applyOrg.*");
		if(null==model) {
			model=new FlowBusiness();
			model.setOid(oid);
			model.setType(type);
			model.setTitle(title);
			model.setBusiService(service);
			model.setApplyUser(new AdminLogin());
			model.getApplyUser().setOid(adminOid);
			model.setApplyOrg(new OrgGroupView());
			model.getApplyOrg().setOid(deptOid);
			model.setCreateDate(new Date());
			model.setDoneStatus("N");
			ModelCheckUtil.checkNotNull(model, "type","title","busiService");
			ModelUpdateUtil.insertModel(model);
		}else if(ObjectUtil.isEmptyOid(model.getCurrentSection())){
			model.setType(type);
			model.setTitle(title);
			model.setBusiService(service);
			model.setApplyUser(new AdminLogin());
			model.getApplyUser().setOid(adminOid);
			model.setApplyOrg(new OrgGroupView());
			model.getApplyOrg().setOid(deptOid);
			ModelUpdateUtil.updateModel(model, "type","title","busiService","applyUser.oid","applyOrg.oid");
		}
		return model;
	}
	/**
	 * 获取流程业务，找不到报错
	 *  oid
	 * 
	 *  Exception
	 */
	public FlowBusiness toFlowBusiness(String oid) throws Exception {
		if(StringUtil.isSpace(oid)) throw new MException(this.getClass(), "业务主键不能为空");
		FlowBusiness model=ModelQueryList.getModel(FlowBusiness.class, oid,"*","applyUser.*","applyOrg.*");
		if(null==model) {
			throw new MException(this.getClass(), "没有找到流程业务");
		}
		return model;
	}
	/**
	 * 更新流程业务的当前环节
	 *  oid
	 *  currentSection
	 *  currentIndex
	 *  MException
	 */
	public void updateCurrentSection(String oid,FlowSection currentSection,Integer currentIndex) throws MException {
		if(null!=currentSection&&!StringUtil.isSpace(currentSection.getOid())) {
			FlowBusiness model=new FlowBusiness();
			model.setOid(oid);
			model.setCurrentSection(currentSection);
			model.setCurrentIndex(currentIndex);
			ModelUpdateUtil.updateModel(model, "currentSection.oid","currentIndex");
		}
	}

	/**
	 * 更新下一步默认人
	 */
	public void updateNextDefaultUser(String oid,String[] admins, boolean selectable) throws MException {
		FlowBusiness model=new FlowBusiness();
		model.setOid(oid);
		model.setNextDefaultUser(admins);
		model.setNextSelectUser(selectable?"Y":"N");
		ModelUpdateUtil.updateModel(model,"nextDefaultUser","nextSelectUser");
	}
	/**
	 * 更新下一步选人限制
	 */
	public void updateNextUser4Limit(String oid,String[] toOrg,String[] toRole) throws MException {
		FlowBusiness model=new FlowBusiness();
		model.setOid(oid);
		model.setToOrgOption("Y");
		model.setToOrg(toOrg);
		model.setToRole(toRole);
		ModelUpdateUtil.updateModel(model,"toOrgOption","toOrg","toRole");
	}
	/**
	 * 更新下一步选人限制
	 */
	public void updateNextUser4Dept(String oid,String[] toRole) throws MException {
		FlowBusiness model=new FlowBusiness();
		model.setOid(oid);
		model.setToOrgOption("A");
		model.setToRole(toRole);
		ModelUpdateUtil.updateModel(model,"toOrgOption","toOrg","toRole");
	}
	/**
	 * 更新下一步选人限制
	 */
	public void updateNextUser4Org(String oid,String[] toRole) throws MException {
		FlowBusiness model=new FlowBusiness();
		model.setOid(oid);
		model.setToOrgOption("B");
		model.setToRole(toRole);
		ModelUpdateUtil.updateModel(model,"toOrgOption","toOrg","toRole");
	}

	/**
	 * 清楚下一步参数 执行流程更新
	 */
	public void clearNextParams(String oid) throws MException {
		FlowBusiness model=new FlowBusiness();
		model.setOid(oid);
		ModelUpdateUtil.updateModel(model,"nextDefaultUser","nextSelectUser","toOrgOption","toOrg","toRole");

	}
	
}
